var tableIns;//表格实例
layui.use(['laydate','form','table'], function(){
    var form = layui.form;
    dict2Select("stockTradeStatus","status");
    form.render();
    var laydate = layui.laydate;
	laydate.render({
		elem: '#startDate'
		, type: 'date'
	});
	laydate.render({
		elem: '#endDate'
		, type: 'date'
	});
    var table = layui.table;
    tableIns = table.render({
        elem: '#listTable'
        , url: appendCtx("/stock/trade/records")
        , even: true
        , cols: [[ //表头
            {field: 'stockCode', title: '股票编码', align: 'center',width: 140},
            {field: 'stockName', title: '股票名称', align: 'center',width: 140},
            {field: 'status', title: '交易状态', align: 'center',width: 100,templet:
            	function(d)
            	{
            		return translate("stockTradeStatus",d.status);
            	}
            },
            {field: 'buyDate', title: '购买日期', align: 'center',width: 140},
            {field: 'buyPrice', title: '购买价格', align: 'center',width: 100,templet:
            	function(d)
            	{
            		if(d.buyPrice)
            			return numberFormat(d.buyPrice,2);
            		return "";
            	}
            },
            {field: 'buyVolume', title: '购买数量', align: 'center',width: 100},
            {field: 'buyAmount', title: '购买总价', align: 'center',width: 100,templet:
            	function(d)
            	{
            		if(d.buyAmount)
            			return numberFormat(d.buyAmount,2);
            		return "";
            	}
            },
            {field: 'sellDate', title: '出售日期', align: 'center',width: 140},
            {field: 'sellPrice', title: '出售价格', align: 'center',width: 100,templet:
            	function(d)
            	{
            		if(d.sellPrice)
            			return numberFormat(d.sellPrice,2);
            		return "";
            	}
            },
            {field: 'sellVolume', title: '出售数量', align: 'center',width: 100},
            {field: 'sellAmount', title: '出售总价', align: 'center',width: 100,templet:
            	function(d)
            	{
            		if(d.sellAmount)
            			return numberFormat(d.sellAmount,2);
            		return "";
            	}
            },
            {field: 'costDays', title: '时间成本', align: 'center',width: 100},
            {field: 'profit', title: '利润', align: 'center',width: 100,templet:
            	function(d)
            	{
            		if(d.profit)
            			if(d.profit>0){
                			return "<b style='color:red'>"+numberFormat(d.profit,2)+"</b>";
            			}else{
                			return "<b style='color:green'>"+numberFormat(d.profit,2)+"</b>";
            			}
            		return "";
            	}
            },
            {field: 'yieldRate', title: '收益率', align: 'center',width: 100,templet:
            	function(d)
            	{
            		if(d.yieldRate)
            			return numberFormat(d.yieldRate,2)+"%";
            		return "";
            	}
            },
            {field: 'annualizedReturn', title: '年化收益率', align: 'center',width: 100,templet:
            	function(d)
            	{
            		if(d.annualizedReturn)
            			return numberFormat(d.annualizedReturn,2)+"%";
            		return "";
            	}
            },
            {field: 'createTime', title: '创建时间',align: 'center',width: 180},
            {title: '操作', align: 'center',width: 200,fixed:"right",toolbar: '#operBar'}
        ]]
    	, page: {
        	limit:10,
        	limits:[10,30,50,100]
        }
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
                ,"attr":res.attr
            };
        },
        done:function (res, curr, count) {
        	var stat=res.attr.stat;
			if(!isNull(stat)){
				$('#buyVSum').html(stat.buyVSum);
				$('#buySum').html(numberFormat(stat.buySum, 2));
				$('#sellSum').html(numberFormat(stat.sellSum, 2));
				$('#holdingAmount').html(numberFormat(stat.holdingAmount, 2));
				$('#holdingVol').html(stat.holdingVol);
				$('#holdingPrice').html(numberFormat(stat.holdingPrice, 2));
				$('#profitSum').html(numberFormat(stat.profitSum, 2));
				$('#yieldRate').html(numberFormat(stat.yieldRate, 2)+"%");
			}
		}
    });
    form.on('submit(search)', function(formData){
        tableIns.reload({
            where:formData.field,
            page:{
                curr: 1
            }
        });
        return false;
    });
    
    form.on('submit(add)', function(){
    	layer.open({
            type: 2,
            title: '新增股票交易',
            area: ['800px', '550px'],
            content: 'tradeDetail.html',
        });
        return false;
    });
});

function edit(id) {
    layer.open({
        type: 2,
        title: '修改股票数据',
        area: ['800px', '550px'],
        content: 'tradeDetail.html?id=' + id,
    });
}

function refreshTableData() {
    tableIns.reload({});
}

function del(id){
	io.delete("/stock/trade/records/"+id,function(res){
		refreshTableData();
	});
}
